/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.img;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

/**
 * <p>Project: app2022 - ImgStr2
 * <p>Powered by webrx On 2022-01-21 15:59:01
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class ImgStr2 {
    public static void main(String[] args) {
        for (int i = 0; i < 50; i++) {
            check();
        }
    }

    public static void check() {
        var len = 4;
        var w = len * 35 - 25;
        var h = 50;
        var t = 2;
        var rand = new Random();
        var i = new BufferedImage(w, h, t);
        var g = i.createGraphics();
        //设置线条和文件的，抗锯齿（平滑）
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        g.setColor(new Color(251, 251, 251, rand.nextInt(230, 256)));
        g.fillRect(0, 0, w, h);

        //设置字号
        int fontsize = 35;

        //设置字体
        var font = new Font("宋体", Font.BOLD, fontsize);
        try {
            var is = Thread.currentThread().getContextClassLoader().getResourceAsStream("fonts/f2.ttf");
            font = Font.createFont(Font.TRUETYPE_FONT, is);
            font = font.deriveFont(Font.BOLD, (float) fontsize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        g.setFont(font);
        var fm = g.getFontMetrics(font);
        //设置验证码
        var letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        var sbu = new StringBuilder();
        for (int j = 0; j < len; j++) {
            g.setColor(new Color(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), 255));
            var x = j * 30 + 5;
            var y = h / 2 + (fm.getAscent() - fm.getDescent()) / 2;
            y = rand.nextInt(y - 15, y + 15);
            var pos = rand.nextInt(letter.length());
            var s = letter.substring(pos, pos + 1);
            sbu.append(s);
            double deg = Math.PI * (rand.nextInt(35) / 180D);
            if (rand.nextBoolean()) {
                deg = -deg;
            }
            g.rotate(deg, x, y);
            g.drawString(s, x, y);
            g.rotate(-deg, x, y);
        }
        var cs = sbu.toString();
        System.out.println(cs);

        //干扰文字
        for (int j = 0; j < len * 5; j++) {
            g.setColor(new Color(rand.nextInt(180, 256), rand.nextInt(180, 256), rand.nextInt(180, 256), rand.nextInt(50, 220)));
            var x = rand.nextInt(w);
            var y = rand.nextInt(h);
            var pos = rand.nextInt(letter.length());
            var s = letter.substring(pos, pos + 1);
            font = font.deriveFont((float) rand.nextInt(12, 52));
            g.setFont(font);
            g.drawString(s, x, y);
        }

        //干扰曲线
        Point[] points = {new Point(0, 0), new Point(10, 50), new Point(30, 6), new Point(60, 55), new Point(80, 3), new Point(160, 60)};

        GeneralPath path = new GeneralPath();
        path.moveTo(points[0].x, points[0].y);
        for (int i2 = 0; i2 < points.length - 1; ++i2) {
            Point sp = points[i2];
            Point ep = points[i2 + 1];
            Point c1 = new Point((sp.x + ep.x) / 2, sp.y);
            Point c2 = new Point((sp.x + ep.x) / 2, ep.y);
            path.curveTo(c1.x, c1.y, c2.x, c2.y, ep.x, ep.y);
        }
        g.setStroke(new BasicStroke(rand.nextInt(6) + 2));
        g.draw(path);

        g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(25) + 220));
        int yy = rand.nextInt(15) + 15;// y
        int hh = rand.nextInt(20) + 10;// 高度
        int aa = rand.nextInt(60) + 20;//
        // x
        for (int x = 10; x < w; x++) {
            int y = (int) (yy + hh * Math.sin(x * Math.PI / aa));
            // g.drawLine(x, (int) y, x, (int) y);
            g.fillOval(x, y, 3, 3);
        }


        g.dispose();
        //保存图像文件
        try {
            String c = UUID.randomUUID().toString().toLowerCase();
            ImageIO.write(i, "png", new File(String.format("%s.png", c)));
        } catch (Exception e) {
            e.printStackTrace();
        }

        //打开图像文件
        try {
            Runtime.getRuntime().exec("cmd /k start i.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
